// // --------------------------------------------------------------------------------------------------------------------
// // <copyright file="DatabaseSettingsForm.cs" company="Collaboris">
// //   Copyright Collaboris
// // </copyright>
// // <summary>
// //   
// // </summary>
// // --------------------------------------------------------------------------------------------------------------------

namespace DocRead.Selenium.Tests.DocRead.ControlObjects
{
    #region

    using Collaboris.ShareTest.Core;
    using Collaboris.ShareTest.SharePoint2010.ControlObjects;

    using OpenQA.Selenium;
    using OpenQA.Selenium.Remote;

    #endregion

    public class DatabaseSettingsForm : SPTModelBase
    {
        #region Constants and Fields

        public static string CtrlDatabaseServerName = "ctl00_PlaceHolderMain_DatabaseServerSection_ServerNameTextBox";

        public static string CtrlDatabaseServerNameError =
            "ctl00_PlaceHolderMain_DatabaseServerSection_ServerNameTextBoxValidator";

        public static string CtrlDocReadDatabaseName =
            "ctl00_PlaceHolderMain_DocReadDatabaseSection_DatabaseNameTextBox";

        public static string CtrlDocReadDatabaseNameError =
            "ctl00_PlaceHolderMain_DocReadDatabaseSection_DatabaseNameTextBoxValidator";

        public static string CtrlOkButton = "ctl00_PlaceHolderMain_ctl00_RptControls_CreateDatabaseButton";

        public static string CtrlSchedulerDatabaseName =
            "ctl00_PlaceHolderMain_SchedulerDatabaseSection_SchedulerDatabaseNameTextBox";

        public static string CtrlSchedulerServicesAccountName =
            "ctl00_PlaceHolderMain_SchedulerDatabaseSection_SchedulerServiceAccountTextBox";

        #endregion

        #region Constructors and Destructors

        public DatabaseSettingsForm(RemoteWebDriver driver)
            : base(driver)
        {
        }

        #endregion

        #region Public Properties

        public string DatabaseServer { get; set; }

        public IWebElement DatabaseServerElement
        {
            get
            {
                return this.WebDriver.FindElementById(CtrlDatabaseServerName);
            }
        }

        public string DatabaseServerErrorMessage { get; set; }

        public IWebElement DatabaseServerErrorMessageElement
        {
            get
            {
                if (this.WebDriver.IsElementPresent(By.Id(CtrlDatabaseServerNameError)))
                {
                    return this.WebDriver.FindElementById(CtrlDatabaseServerNameError);
                }
                return null;
            }
        }

        public string DocReadDatabaseName { get; set; }

        public IWebElement DocReadDatabaseNameElement
        {
            get
            {
                return this.WebDriver.FindElementById(CtrlDocReadDatabaseName);
            }
        }

        public string DocReadDatabaseNameErrorMessage { get; set; }

        public IWebElement DocReadDatabaseNameErrorMessageElement
        {
            get
            {
                if (this.WebDriver.IsElementPresent(By.Id(CtrlDocReadDatabaseNameError)))
                {
                    return this.WebDriver.FindElementById(CtrlDocReadDatabaseNameError);
                }
                return null;
            }
        }

        public IWebElement OkButton
        {
            get
            {
                return this.WebDriver.FindElementById(CtrlOkButton);
            }
        }

        public string SchedulerDatabaseName { get; set; }

        public IWebElement SchedulerDatabaseNameElement
        {
            get
            {
                return this.WebDriver.FindElementById(CtrlSchedulerDatabaseName);
            }
        }

        public string SchedulerServicesAccount { get; set; }

        public IWebElement SchedulerServicesAccountElement
        {
            get
            {
                return this.WebDriver.FindElementById(CtrlSchedulerServicesAccountName);
            }
        }

        public string TopLevelError { get; set; }

        public IWebElement TopLevelErrorElement
        {
            get
            {
                return this.WebDriver.IsElementPresent(By.XPath(Locators.XpathTopLevelErrorIdentifier))
                           ? this.WebDriver.FindElementByXPath(Locators.XpathTopLevelErrorIdentifier)
                           : null;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// 	Refresh Form
        /// </summary>
        public void RefreshForm()
        {
            this.DatabaseServer = this.DatabaseServerElement.GetAttribute("value");
            this.DocReadDatabaseName = this.DocReadDatabaseNameElement.GetAttribute("value");
            this.SchedulerDatabaseName = this.SchedulerDatabaseNameElement.GetAttribute("value");
            this.SchedulerServicesAccount = this.SchedulerServicesAccountElement.GetAttribute("value");

            if (this.DocReadDatabaseNameErrorMessageElement != null)
            {
                this.DocReadDatabaseNameErrorMessage = this.DocReadDatabaseNameErrorMessageElement.Text;
            }

            if (this.DatabaseServerErrorMessageElement != null)
            {
                this.DatabaseServerErrorMessage = this.DatabaseServerErrorMessageElement.Text;
            }

            if (this.TopLevelErrorElement != null)
            {
                this.TopLevelError = this.TopLevelErrorElement.Text;
            }
        }

        #endregion
    }
}